//
//  HDPercentageView.m
//  HRSNetWorking
//
//  Created by huangrensheng on 16/5/3.
//  Copyright © 2016年 laber. All rights reserved.
//

#import "HDPercentageView.h"
#import "Masonry.h"

#define RGBACOLOR(r, g, b, a)   [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

@implementation HDPercentageView


- (instancetype)init{
    if ([super init]) {
        [self setBackgroundColor:[UIColor colorWithRed:237/255.0f green:233/255.0f blue:231/255.0f alpha:1]];
        self.alreadyAuditCount = 5;
        self.totalCount = 10;
        
        [self initView];
    }
    return self;
}


- (void)initView{
    __weak typeof(self) weakSelf = self;
    
    UIView *centerView = [[UIView alloc] init];
    [self addSubview:centerView];
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@72);
        make.height.equalTo(@72);
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.centerY.equalTo(weakSelf.mas_centerY);
    }];
    
    UILabel *count = [[UILabel alloc] init];
    count.textAlignment = NSTextAlignmentCenter;
    count.text = [NSString stringWithFormat:@"%ld",(long)self.totalCount];
    count.font = [UIFont boldSystemFontOfSize:30];
    count.textColor = RGBACOLOR(100, 103, 102, 1);
    [centerView addSubview:count];
    [count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@30);
        make.bottom.equalTo(centerView.mas_centerY).equalTo(@5);
        make.centerX.equalTo(centerView.mas_centerX);
    }];
    
    UILabel *countMessage = [[UILabel alloc] init];
    countMessage.textAlignment = NSTextAlignmentCenter;
    if (self.alreadyAuditCount == 0) {
        countMessage.text = @"待审核";
    }else if(self.alreadyAuditCount == self.totalCount){
        countMessage.text = @"已审核";
    }else{
        countMessage.text = @"已邀请";
    }
    countMessage.font = [UIFont boldSystemFontOfSize:13];
    countMessage.textColor = RGBACOLOR(118, 119, 120, 1);
    [centerView addSubview:countMessage];
    [countMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(centerView.mas_centerX);
        make.top.equalTo(centerView.mas_centerY).equalTo(@5);
    }];
    
}


- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();//获取一个画布,可以绘制任意图形

    /*已审核*/
    CGContextSetLineWidth(context, 22.0);//线的宽度
    CGContextSetRGBStrokeColor(context, 135/255.0f, 197/255.0f, 55/255.0f, 1);//改变画笔颜色
    double aa = (self.alreadyAuditCount/self.totalCount)*360;
    NSLog(@"aa %.02f",aa);
    if (self.alreadyAuditCount == 0) {
        
    }else if(self.alreadyAuditCount == self.totalCount){
        CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 72, 0, 2*M_PI, 0); //添加一个圆
    }else{
        CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 72, -88.2*M_PI/180, (aa-90-1.8)*M_PI/180, 0); //添加一个圆
    }
    CGContextDrawPath(context, kCGPathStroke); //绘制路径
    

    /*待审核*/
    CGContextSetLineWidth(context, 22.0);//线的宽度
    CGContextSetRGBStrokeColor(context, 245/255.0f, 142/255.0f, 35/255.0f, 1);
    if (self.alreadyAuditCount == 0) {
        CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 72, 0, 2*M_PI, 0); //添加一个圆
    }else if(self.alreadyAuditCount == self.totalCount){
        
    }else{
        CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 72, -91.8*M_PI/180, (aa-90+1.8)*M_PI/180, 1); //添加一个圆
    }
    
    CGContextDrawPath(context, kCGPathStroke); //绘制路径
    
    /*中心半透明圆*/
    //画大圆并填充颜
    UIColor*aColor = RGBACOLOR(255, 255, 255, 0.3);
    CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
    CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 68, 0, 2*M_PI, 0); //添加一个圆
    //kCGPathFill填充非零绕数规则,kCGPathEOFill表示用奇偶规则,kCGPathStroke路径,kCGPathFillStroke路径填充,kCGPathEOFillStroke表示描线，不是填充
    CGContextDrawPath(context, kCGPathFill); //绘制路径加填充
}


//-(CGPoint)calcCircleCoordinateWithCenter:(CGPoint) center  andWithAngle : (CGFloat) angle andWithRadius: (CGFloat) radius{
//    CGFloat x2 = radius*cosf(angle*M_PI/180);
//    CGFloat y2 = radius*sinf(angle*M_PI/180);
//    NSLog(@"x2---%0.2f   y2----%0.2f",x2,y2);
//    if (angle*2 <= 90) {
//        return CGPointMake(center.x+y2+22, center.y-x2);
//    }else{
//        return CGPointMake(center.x+y2+22, center.y-x2);
//    }
//}

@end
